'''
Created on 7/01/2013

@author: Jorge
'''
from Classifier import Classifier
import numpy as np
import matplotlib.pyplot as plt
import mlpy
from dataset.DatasetImplementations import DatasetForLibSVM
from classifiers.svm_tree import SVMTree

class ClassificationTree(Classifier):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        
    def train(self, train_set):
        
        X=[]
        upper_Y=[]
        for e in train_set:
            X.append(e.get_vector_X())
            upper_Y.append(e.get_numerical_upper_y())
        
        x = np.array(X)
        y = np.array(upper_Y)
        self.tree = mlpy.ClassTree(minsize=10)
        self.tree.learn(x, y)
        
    def test(self, test_set):
        
        X=[]
        upper_Y=[]
        for e in test_set:
            X.append(e.get_vector_X())
            upper_Y.append(e.get_numerical_upper_y())
        
        x = np.array(X)
        y = np.array(upper_Y)
        labels  = self.tree.pred(x)
        
        count=0
        for i in range(len(labels)):
            if labels[i]== 0: labels[i]=-1
            if y[i]== 0: y[i]=-1
        
        print 'error_p ', mlpy.error_p(y, labels)
        print 'error_n ', mlpy.error_n(y, labels)
        

if __name__ == '__main__':
    classifier = ClassificationTree()
    
    data = DatasetForLibSVM()
    training_set = data.get_training_set()
    training_set.extend(data.get_validation_set())
    test_set = data.get_test_set()
    
    training_set, test_set =  SVMTree.convert_example(training_set, test_set)
    
    classifier.train(training_set)
    classifier.test(test_set)